package com.passive.lock.controller.user;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.controller.BaseController;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TRegionVo;
import com.passive.lock.service.user.RegionService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("re")
public class RegionController extends BaseController {
	@Autowired
	private RegionService regionService;

	/**
	 * 区域信息管理
	 *
	 */
	
	@RequestMapping(value = "/parentregion")
	@ResponseBody
	public Result parentRegion(@RequestParam Integer regionid) {
		try {
			Result result = regionService.parentRegion(regionid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	@RequestMapping(value = "/queryregion")
	@ResponseBody
	public Result queryRegion(@RequestParam Integer regionid) {
		try {
			Result result = regionService.queryRegion(regionid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	
	
	@RequestMapping(value = "/childrenregion")
	@ResponseBody
	public Result childrenRegion(@RequestParam Integer parentid) {
		try {
			Result result = regionService.childrenRegion(parentid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/regionlist")
	@ResponseBody
	public Result regionList(@RequestParam Integer parentid, @RequestParam String regionName,
			@RequestParam Integer page, @RequestParam Integer limit) {
		Integer offset = PageUtil.getOffset(page, limit);
		try {
			List<TRegionVo> list = regionService.regionList(parentid, regionName, offset, limit);
			int count = regionService.regionListCount(parentid, regionName);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addregion")
	@ResponseBody
	public Result addRegion(@RequestBody TRegionVo tRegionVo) {
		try {
			Result result = regionService.addRegion(tRegionVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updateRegion")
	@ResponseBody
	public Result updateRegion(@RequestBody TRegionVo tRegionVo) {
		try {
			Result result = regionService.updateRegion(tRegionVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteregion")
	@ResponseBody
	public Result deleteRegion(@RequestBody TRegionVo tRegionVo) {
		try {
			Result result = regionService.deleteRegion(tRegionVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteregionlist")
	@ResponseBody
	public Result deleteRegionList(@RequestBody List<Integer> regionids) {
		try {
			Result result = regionService.deleteRegionList(regionids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

}
